/**
 * 
 *
 * @(#)registService.java 2015-3-22
 * @author WeiNing Liu
 * @version 0.1
 * @since JDK 7.0
 */

package model.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import dom.domain.User;
import dom.domain.UserDAO;

//import net.sf.json.JSONObject;

public class registService {
	private UserDAO userDao;
	private Map<String, String> map;
	private User user;

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	public String regist(String userInfo) {
		map = new HashMap<String, String>();
		try {
			JSONObject registUser = JSONObject.fromObject(userInfo);

			if (registUser.containsKey("userName")
					&& !registUser.getString("userName").equals("")) {
				user.setName(registUser.getString("userName"));
				if (registUser.containsKey("psd")
						&& !registUser.getString("psd").equals("")) {
					user.setPsd(registUser.getString("psd"));
					if (registUser.containsKey("id")
							&& !registUser.getString("id").equals("")) {
						user.setId(registUser.getString("id"));
						if (registUser.containsKey("email")
								&& !registUser.getString("email").equals("")) {
							if(!(registUser.containsKey("eeid")
									&& !registUser.getString("eeid").equals("")))
							{
								map.put("flag", "false");
								map.put("Info", "高考号为空");
								return JSONObject.fromObject(map).toString();
							}
							user.setEeid(registUser.getString("eeid"));
							user.setEpAllocated(0);
							user.setUserId(registUser.getString("email"));
							User temp = userDao.findById(registUser.getString("email"));
							if (temp != null) {
								map.put("flag", "false");
								map.put("Info", "用户名已存在");
								return JSONObject.fromObject(map).toString();
							}
							user.setEmail(registUser.getString("email"));
							user.setProvinceId(Integer.parseInt(registUser.getString("id").substring(0,2)));
//							user.setBirthday(constant.constant.c.getTime()
//									.toString());
							user.setEpyear(constant.constant.year);
//							user.setPolicy("无");
							user.setState(UserService.registState);
							user.setAdmitIdAllocated(0);
//							if (registUser.containsKey("nation")) {
//								user.setNation(registUser.getString("nation"));
//							} else {
//								user.setNation("汉");
//							}
							// user.set
							userDao.save(user);
							map.put("flag", "true");
							map.put("info", "regist success!");
						} else {
							map.put("flag", "false");
							map.put("info", "email is empty");
						}
					} else {
						map.put("flag", "false");
						map.put("info", "id is empty");
					}
				} else {
					map.put("flag", "false");
					map.put("info", "password is empty");

				}
			} else {
				map.put("flag", "false");
				map.put("info", "username is empty");
			}

		} catch (JSONException e) {
			map.put("flag", "false");
			map.put("info", e.getMessage());
		}
		// map = new HashMap<String,String>();
		// map.put("flag", "true");

		return JSONObject.fromObject(map).toString();
	}

}
